import {h, markRaw} from "vue";
import {getBusinessObject} from "bpmn-js/lib/util/ModelUtil";

export default function (wrapped) {
    return {
        inject: ["modelerObj"],
        props: {
            bpmnElement: {
                type: Object
            }
        },
        data() {
            return {
                modeler: null,
                businessObj: null,
                flag: false
            }
        },
        methods: {
            // 数据初始化
            async initData(){
                const fn = this.$refs.cmpRef.initData;
                fn && await fn();
            },
            // 数据回填
            dataReset() {
                this.$refs.cmpRef.bpmnElement = this.bpmnElement;
                this.$refs.cmpRef.businessObj = getBusinessObject(this.bpmnElement);
                this.$nextTick(() => {
                    const fn = this.$refs.cmpRef.dataReset;
                    fn && fn();
                })
            }
        },
        watch: {
            "bpmnElement": function () {
                if(!this.flag) return;
                this.dataReset();
            },
        },
        async mounted() {
            this.$refs.cmpRef.modeler = markRaw(this.modelerObj.modeler);
            await this.initData();
            this.dataReset();
            this.flag = true;
        },
        render() {
            return h(wrapped, {
                ref: "cmpRef"
            })
        }
    }
}